<?php get_header(); 
    $keyword = trim(get_search_query());
    if($keyword)
    {
        global $wpdb;
        $rs = $wpdb->get_results($query);
    }
?>
<div class="row-fluid">
            <div class="span8 content-sidebar">
                <div class="row-fluid boxf">
                        <div class="row-fluid header-coupon">
                            <div class="span12 code-coupon"><h2><?php cpx_highligh_keyword("Stores name match with ".$keyword, $keyword); ?></h2></div>
                        </div>
                        <?php 
                        if($rs)
                        {
                            foreach ($rs as $obj) {
                                $st_id = $obj->ID;                             
                                cpx_get_store_detail($st_id, $keyword);
                            }
                        }
                        else
                        {
                            echo "<p class='search_not_found'>No stores match your search \"$keyword\"</p>";
                        }
                        ?>
                </div> 
                        <?php  
                        $t_events = get_terms('event', array('hide_empty' => false));
                        $term_matches = array();
                        // Find term name matching
                        foreach ($t_events as $obj) {
                            // Search keyword in event name
                            $pos = strpos(strtolower($obj->name),$keyword);
                            if(is_integer($pos))
                                array_push($term_matches, $obj);                                
                        }
                        // Print all event title match                        
                        foreach ($term_matches as $t) { ?>
                        <div class="row-fluid boxf">
                        <div class="row-fluid header-coupon">
                            <div class="span12 code-coupon"><h2><?php cpx_highligh_keyword("Coupons in ".$t->name,$keyword); ?></h2></div>
                        </div>
                        <?php
                            $args = array(
                            'post_type'=>'coupon',
                            'post_status'=>'publish',
                            'taxonomy'=>'event',
                            'term'=>$t->slug,
                            'posts_per_page'=>5
                            );
                            $arr_cp = query_posts($args);
                            if(count($arr_cp) > 0)
                            {
                                foreach ($arr_cp as $p) {
                                    //print coupon
                                    $comment_count = wp_count_comments($p->ID);
                                    echo cpx_get_coupon_detail($p->ID, $comment_count->total_comments, true, $keyword);
                                }
                                // print view more link
                                $term_link = get_term_link($t,'event');
                                echo "<a class='search_view_more' href='$term_link' title='{$t->name}' >View more {$t->name} coupons &gt;&gt;</a>";
                            }
                            else
                                echo '<p class="search_not_found">No coupons found.</p>'; ?>
                        </div>
                        <?php    
                        } // end foreach event matches
                        ?>
            </div>
            <div class="span4 right-sidebar">
                    <?php if(is_active_sidebar('cs-main-sidebar')) dynamic_sidebar( 'cs-main-sidebar' ); ?>
            </div>
</div>
<?php get_footer(); ?>